﻿/*
* Copyright (c) 2008 michiyasu wada
* http://www.seyself.com/
* 
* Distributed under The MIT License.
* [http://www.opensource.org/licenses/mit-license.php]
*/

//****************************************************************************
// ActionScript Standard Library
// flash.geom.Transform object
//****************************************************************************

import flash7.geom.Matrix;
import flash7.geom.Rectangle;
import flash7.geom.ColorTransform;

class flash7.geom.Transform
{
    
    public function get matrix():Matrix
    {
        return _Mt;
    }
    public function set matrix( value:Matrix ):Void
    {
        _Mt = value;
        var ts:Number = value.a * value.d - value.b * value.c;
        var sx:Number = Math.sqrt(value.a * value.a + value.c * value.c);
        var sy:Number = ts / sx;
        var angle:Number = Math.acos( value.a / sx );
        
        target._xscale = sx * 100;
        target._yscale = sy * 100;
        target._rotation = angle / Math.PI * 180;
        target._x = value.tx;
        target._y = value.ty;
    }
    
    public function get colorTransform():ColorTransform
    {
        return _CT;
    }
    public function set colorTransform( value:ColorTransform ):Void
    {
        var color:Color = new Color( target );
        var ct:Object = { };
        ct.ra = value.redMultiplier   * 100;
        ct.ga = value.greenMultiplier * 100;
        ct.ba = value.blueMultiplier  * 100;
        ct.aa = value.alphaMultiplier * 100;
        ct.rb = value.redOffset;
        ct.gb = value.greenOffset;
        ct.bb = value.blueOffset;
        ct.ab = value.alphaOffset;
        color.setTransform( ct );
        _CT = value;
    }
    
    public function get concatenatedMatrix():Matrix
    {
        return null;
    }
    
    public function get concatenatedColorTransform():ColorTransform
    {
        return null;    
    }
    
    public function get pixelBounds():Rectangle
    {
        var bounds:Object = target.getBounds();
        var w:Number = bounds.xMax - bounds.xMin;
        var h:Number = bounds.yMax - bounds.yMin;
        return new Rectangle( target._x, target._y, w, h );
    }
    
    public function Transform( mc:MovieClip )
    {
        _init( mc );
    }
    
    
    private var target:MovieClip;
    private var _Mt:Matrix;
    private var _CT:ColorTransform;
    
    private function _init( mc )
    {
        mc["transform"] = this;
        this.target = mc;
        
        _Mt = new Matrix();
        _Mt.createBox( mc._xscale / 100, mc._yscale / 100, mc._rotation / 180 * Math.PI, mc._x, mc._y );
        
        
        var color:Color = new Color( mc );
        var ct = color.getTransform();
        _CT = new ColorTransform( ct.ra/100, ct.ga/100, ct.ba/100, ct.aa/100 ,
                            ct.rb, ct.gb, ct.bb, ct.ab );
        
    }
    
    
}